package indi.zhifa.engine.test2.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource busyDataSource(){
        MultiDataSource multiDataSource = new MultiDataSource();
        multiDataSource.init();
        return multiDataSource;
    }

    public class MultiDataSource extends AbstractRoutingDataSource{

        public static ThreadLocal<String> curKey = new ThreadLocal<>();

        public void init(){
            HikariDataSource dataSourceMonitor = DataSourceBuilder.create().type(HikariDataSource.class)
                    .driverClassName("com.mysql.cj.jdbc.Driver")
                    .url("jdbc:mysql://192.168.0.64:3306/study2024-class007-monitor?useUnicode=true&characterEncoding=utf-8")
                    .username("dbMgr")
                    .password("qqhilvMgAl@7")
                    .build();
            HikariDataSource dataSource1 = DataSourceBuilder.create().type(HikariDataSource.class)
                    .driverClassName("com.mysql.cj.jdbc.Driver")
                    .url("jdbc:mysql://192.168.0.64:3306/study2024-class007-busy001?useUnicode=true&characterEncoding=utf-8")
                    .username("dbMgr")
                    .password("qqhilvMgAl@7")
                    .build();
            HikariDataSource dataSource2 = DataSourceBuilder.create().type(HikariDataSource.class)
                    .driverClassName("com.mysql.cj.jdbc.Driver")
                    .url("jdbc:mysql://192.168.0.64:3306/study2024-class007-busy002?useUnicode=true&characterEncoding=utf-8")
                    .username("dbMgr")
                    .password("qqhilvMgAl@7")
                    .build();
            Map<Object, Object> targetDataSources = new HashMap<>();
            targetDataSources.put("monitor", dataSourceMonitor);
            targetDataSources.put("busy001", dataSource1);
            targetDataSources.put("busy002", dataSource2);
            setTargetDataSources(targetDataSources);
            setDefaultTargetDataSource(dataSourceMonitor);

            curKey.set("monitor");
        }

        @Override
        protected Object determineCurrentLookupKey() {
            return curKey.get();
        }

        public static void changeDb(String pKey){
            curKey.set(pKey);
        }
    }

}
